import sys
import pygame
from pygame.locals import * #@UnusedWildImport

from MainMenu import MainMenu
from Game import Game
from Edit import Edit

class Explore:
    
    def __init__(self, size=(640,480), fill=(0,0,0)):
        pygame.init()
        self.wsize=size
        self.screen = pygame.display.set_mode(self.wsize, 0, 32)
        self.screen.fill(fill)
        self.clock = pygame.time.Clock()
        self.fps=30
        self.version="v0.0.1"
        self.state="MAIN_MENU"
        self.menu = MainMenu()
        self.edit = Edit()
        
    def handleEvents(self):
        if self.state is "IN_GAME":
            if self.game.handleEvents() == False:
                self.state = "MAIN_MENU"
                self.menu.inGameMenu()
        elif self.state is "LEVEL_EDIT":
            if self.edit.handleEvents() == False:
                self.state = "MAIN_MENU"
                self.wsize=(640,480)
                pygame.display.set_mode(self.wsize, 0 , 32)        
        elif self.state is "MAIN_MENU":
            if self.menu.handleEvents() == False:
                self.state = "QUIT"
                self.running = False
        
    def update(self):
        if self.state is "IN_GAME":
            self.game.update(self.screen)
        elif self.state is "LEVEL_EDIT":
            self.edit.update()
        elif self.state is "MAIN_MENU":
            state = self.menu.update()
            if state != 0:
                if state is 1:
                    self.game.save()
                    self.state = "IN_GAME"
                elif state is 2:
                    self.state = "CONTINUE"
                elif state is 3:
                    self.state = "NEW_GAME"
                elif state is 4:
                    self.state = "LOAD_GAME"
                elif state is 5:
                    self.state = "LEVEL_EDIT"
                    self.wsize=(640,640)
                    pygame.display.set_mode(self.wsize, 0 , 32)
                elif state is 6:
                    self.state = "QUIT"
                    
        elif self.state is "NEW_GAME":
            self.state = "IN_GAME"
            self.game = Game()
        elif self.state is "CONTINUE":
            self.state = "IN_GAME"
            self.game = Game()
        elif self.state is "LOAD_GAME":
            self.state = "IN_GAME"
            self.game = Game()
        if self.state is "QUIT":
            self.running = False
            
    def display(self):
        if self.state is "IN_GAME":
            self.game.display()
        elif self.state is "MAIN_MENU":
            self.menu.display(self.screen)
        elif self.state is "LEVEL_EDIT":
            self.edit.display()
            
    def gameLoop(self):
        self.running=True
        
        while self.running:
            pygame.display.set_caption("Explore " + self.version + " - FPS: %i" % self.clock.get_fps())
            self.handleEvents()
            self.update()
            self.display()
            pygame.display.flip()
            self.clock.tick(self.fps)
        
if __name__ == "__main__":
    MainWindow = Explore()
    MainWindow.gameLoop()
    pygame.quit()
    sys.exit()
        
